#include "../../include/InvertIndexMaker.h"
#include <math.h>
#include <iostream>

using std::cout;
using std::endl;

InvertIndexMaker::InvertIndexMaker(vector<WebPage>& webPages, unordered_map<string, unordered_map<int, double>>& invertIndexTable) 
: _webPages(webPages), _invertIndexTable(invertIndexTable)
{

}
void InvertIndexMaker::createTable() {

    _totleWeightforOnePage.resize(_webPages.size(), 0.0);
    for (auto &page : _webPages) {

        map<string, int> &wordsFreq = page.getWordsFreq();  //单个doc的词频统计
        int totalWord = page.getTotalWordsFreq();  //单个doc中出现的总词语数量

        for (auto &wordElem : wordsFreq) {
            double TF = static_cast<double>(wordElem.second) / totalWord;
            //cout << "[wordElem.first]:" << wordElem.first << endl;
            _invertIndexTable[wordElem.first].insert(std::make_pair(page.getDocId(), TF));
       
        }       
    }

    // for(auto & elem : _invertIndexTable) {
    //     cout << elem.first;
    //     for (auto & elem1 : elem.second) {
    //         cout << " " << elem1.first << " " << elem1.second;
    //     }
    //     cout << endl;
    // }
    // while(1);
    for (auto & invertIndexElem : _invertIndexTable) {
        //该词语在每篇doc中权重的集合
        unordered_map<int, double> & docIdOfweight = invertIndexElem.second;
        for (auto &docIdElem : docIdOfweight) {
            double TF = docIdElem.first;
            int DF = docIdOfweight.size();
            int N = _webPages.size();
            double IDF = log2((double)(N) / (DF + 1));

            double w = TF * IDF;
            docIdElem.second = w;
            _totleWeightforOnePage[docIdElem.first - 1] += w * w;
        }
    }

    for (auto & invertIndexElem : _invertIndexTable) {
        unordered_map<int, double> & docIdOfweight = invertIndexElem.second;
        for (auto &docIdElem : docIdOfweight) {
            double sumW = _totleWeightforOnePage[docIdElem.first - 1 ]; //单个doc中词语 w 的总和
            docIdElem.second /= sqrt(sumW);  //生成w`
        }
    }
    
    std::cout << "invertIndexTable make succese!" << std::endl;
}